/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import ManejoExc.PersonalException;
import Negocio.Empresa;
import Negocio.Personal;
import Negocio.Rol;
import Negocio.TipoRol;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;


/**
 *
 * @author Diego
 */
public class FrmPersonal extends javax.swing.JDialog {
    
    private Empresa unaEmpresa;
    private Personal unPersonal;    
    private  DefaultListModel modelPersonal = new DefaultListModel();
    private TipoRol unTipoRol;
    

    /**
     * Creates new form FrmPersonal
     */
    public FrmPersonal(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }    
    
    public FrmPersonal(java.awt.Frame parent, boolean modal,Empresa unaEmpresa) {
        super(parent, modal);
        initComponents();
        this.unaEmpresa = unaEmpresa;
        this.LstPersonal.setModel(modelPersonal);
        this.cargarPersonal();
        
    }
    
    /*
     * Mis Metodos
     */
    
   public  void ponerTipoRol(TipoRol unTipo){
       this.unTipoRol  = unTipo;       
   }
    public void limpiarControles(){
        this.Txt_Apellido.setEnabled(false);
        this.Txt_Cuil.setEnabled(false);
        this.Txt_Dni.setEnabled(false);
        this.Txt_Nombre.setEnabled(false);
        this.Txt_Apellido.setText("");
        this.Txt_Cuil.setText("");
        this.Txt_Dni.setText("");
        this.Txt_Nombre.setText("");
        this.BtnBorrar.setEnabled(false);
        this.BtnCancelar.setEnabled(false);
        this.BtnGuardar.setEnabled(false);
        this.BtnNuevo.setEnabled(true);
        this.cmbFecha.setText("");
    }
    public void cargarPersonal(){
        this.modelPersonal.clear();
        if(this.unaEmpresa.getEmpleadosActivos() != null && this.unaEmpresa.getEmpleadosActivos().size() > 0){
       
        for (Map.Entry personal : this.unaEmpresa.getEmpleadosActivos().entrySet()) {
                this.modelPersonal.addElement((Personal) personal.getValue());
                
            }
        }else{
            this.modelPersonal.addElement("No hay Personal Activo");
        }
    }
   
    
    /*
     * Fin Mis Metodos
     */

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane3 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        Pnl_DatosPersonales = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        Txt_Nombre = new javax.swing.JTextField();
        Txt_Apellido = new javax.swing.JTextField();
        cmbFecha = new datechooser.beans.DateChooserCombo();
        Txt_Dni = new javax.swing.JFormattedTextField();
        Txt_Cuil = new javax.swing.JFormattedTextField();
        BtnNuevo = new javax.swing.JButton();
        BtnGuardar = new javax.swing.JButton();
        BtnBorrar = new javax.swing.JButton();
        BtnCancelar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        LstPersonal = new javax.swing.JList();
        jLabel8 = new javax.swing.JLabel();

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(jList1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        Pnl_DatosPersonales.setEnabled(false);

        jLabel1.setText("Nombre");

        jLabel2.setText("Apellido");

        jLabel3.setText("Cuil");

        jLabel4.setText("DNI");

        jLabel5.setText("Fecha Nacimiento");

        Txt_Nombre.setEnabled(false);

        Txt_Apellido.setEnabled(false);

        Txt_Dni.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("########"))));
        Txt_Dni.setEnabled(false);

        try {
            Txt_Cuil.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##-########-#")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        Txt_Cuil.setEnabled(false);

        BtnNuevo.setText("Nuevo");
        BtnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnNuevoActionPerformed(evt);
            }
        });

        BtnGuardar.setText("Guardar");
        BtnGuardar.setEnabled(false);
        BtnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnGuardarActionPerformed(evt);
            }
        });

        BtnBorrar.setText("Borrar");
        BtnBorrar.setEnabled(false);
        BtnBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnBorrarActionPerformed(evt);
            }
        });

        BtnCancelar.setText("Cancelar");
        BtnCancelar.setEnabled(false);
        BtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(52, 52, 52)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cmbFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Txt_Nombre, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                    .addComponent(Txt_Apellido)
                    .addComponent(Txt_Dni)
                    .addComponent(Txt_Cuil))
                .addContainerGap(45, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(BtnNuevo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(BtnGuardar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BtnBorrar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BtnCancelar)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(Txt_Nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(Txt_Apellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(Txt_Dni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(Txt_Cuil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(cmbFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtnNuevo)
                    .addComponent(BtnGuardar)
                    .addComponent(BtnBorrar)
                    .addComponent(BtnCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        LstPersonal.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        LstPersonal.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LstPersonalMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(LstPersonal);

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel8.setText("Nomina");

        javax.swing.GroupLayout Pnl_DatosPersonalesLayout = new javax.swing.GroupLayout(Pnl_DatosPersonales);
        Pnl_DatosPersonales.setLayout(Pnl_DatosPersonalesLayout);
        Pnl_DatosPersonalesLayout.setHorizontalGroup(
            Pnl_DatosPersonalesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(Pnl_DatosPersonalesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(Pnl_DatosPersonalesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2)
                    .addGroup(Pnl_DatosPersonalesLayout.createSequentialGroup()
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 122, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23))
        );
        Pnl_DatosPersonalesLayout.setVerticalGroup(
            Pnl_DatosPersonalesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(Pnl_DatosPersonalesLayout.createSequentialGroup()
                .addGroup(Pnl_DatosPersonalesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(Pnl_DatosPersonalesLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(Pnl_DatosPersonalesLayout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 2, Short.MAX_VALUE)
                .addComponent(Pnl_DatosPersonales, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 3, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 14, Short.MAX_VALUE)
                .addComponent(Pnl_DatosPersonales, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void LstPersonalMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LstPersonalMouseClicked
        // TODO add your handling code here:
        this.unPersonal = (Personal) this.modelPersonal.getElementAt(this.LstPersonal.getSelectedIndex());
        this.Txt_Apellido.setText(this.unPersonal.getApellido());
        this.Txt_Cuil.setText(this.unPersonal.getCuil());
        this.Txt_Dni.setText(this.unPersonal.getDni());
        this.Txt_Nombre.setText(this.unPersonal.getNombre());
        this.Txt_Nombre.setEnabled(true);
        this.Txt_Cuil.setEnabled(true);
        this.Txt_Dni.setEnabled(true);
        this.Txt_Nombre.setEnabled(true);
        this.Txt_Apellido.setEnabled(true);
        this.BtnBorrar.setEnabled(true);
        this.BtnCancelar.setEnabled(true);
        this.BtnGuardar.setEnabled(true);
        this.BtnNuevo.setEnabled(false);
        this.unTipoRol = this.unaEmpresa.RolEmpleado(this.unPersonal);

        
        
    }//GEN-LAST:event_LstPersonalMouseClicked

    private void BtnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnNuevoActionPerformed
        // TODO add your handling code here:
        this.unPersonal = null;
        this.Txt_Apellido.setEnabled(true);
        this.Txt_Cuil.setEnabled(true);
        this.Txt_Dni.setEnabled(true);
        this.Txt_Nombre.setEnabled(true);
        
        this.Txt_Apellido.setText("");        
        this.Txt_Cuil.setText("");        
        this.Txt_Dni.setText("");        
        this.Txt_Nombre.setText("");       
        
        this.BtnBorrar.setEnabled(false);
        this.BtnCancelar.setEnabled(true);
        this.BtnGuardar.setEnabled(true);
        this.BtnNuevo.setEnabled(true);
        
    }//GEN-LAST:event_BtnNuevoActionPerformed

    private void BtnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnGuardarActionPerformed
        // TODO add your handling code here:
       
        try{  
           if(this.unPersonal != null){
               this.unaEmpresa.modificarPersonal(this.unPersonal,this.Txt_Apellido.getText(),this.Txt_Cuil.getText(),this.Txt_Dni.getText(),Empresa.miUtilidadDeFechas.fechaDb(this.cmbFecha.getText()),this.Txt_Nombre.getText());
           }else{
            this.unaEmpresa.nuevoPersonal(this.Txt_Apellido.getText(),this.Txt_Cuil.getText(),this.Txt_Dni.getText(),Empresa.miUtilidadDeFechas.fechaDb(this.cmbFecha.getText()),this.Txt_Nombre.getText());
           }
            JOptionPane.showMessageDialog(this, "La operacion se realizo correctamente");
            this.cargarPersonal();
            this.limpiarControles();

        }
        catch(PersonalException ex){
            JOptionPane.showMessageDialog(this, ex.getMessage());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
            
    }//GEN-LAST:event_BtnGuardarActionPerformed

    private void BtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCancelarActionPerformed
        // TODO add your handling code here:
        this.limpiarControles();
        this.unPersonal = null;

    }//GEN-LAST:event_BtnCancelarActionPerformed

    private void BtnBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnBorrarActionPerformed
        // TODO add your handling code here:
        
        int mc,bc,ch=1;
        mc = JOptionPane.QUESTION_MESSAGE;
        bc = JOptionPane.YES_NO_CANCEL_OPTION;
        ch = JOptionPane.showConfirmDialog (this, "Desea dar de baja al empleado?", "Confirmacion", bc, mc);
        if(ch == 0 && this.unPersonal != null){                       
            this.unaEmpresa.bajaEmpleado(this.unPersonal);
            this.unPersonal = null;
            this.limpiarControles();
            this.cargarPersonal();            
        }
    }//GEN-LAST:event_BtnBorrarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void mains(String args[]) {
    
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnBorrar;
    private javax.swing.JButton BtnCancelar;
    private javax.swing.JButton BtnGuardar;
    private javax.swing.JButton BtnNuevo;
    private javax.swing.JList LstPersonal;
    private javax.swing.JPanel Pnl_DatosPersonales;
    private javax.swing.JTextField Txt_Apellido;
    private javax.swing.JFormattedTextField Txt_Cuil;
    private javax.swing.JFormattedTextField Txt_Dni;
    private javax.swing.JTextField Txt_Nombre;
    private datechooser.beans.DateChooserCombo cmbFecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables
}
